//
//  OrderCell.m
//  WorkNest
//
//  Created by qbm360 on 15/7/13.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "OrderCell.h"

@interface OrderCell ()

@property (weak, nonatomic) IBOutlet UIButton *checkButton;
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *productName;
@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UILabel *count;
@property (weak, nonatomic) IBOutlet UILabel *totalPrice;
@property (weak, nonatomic) IBOutlet UIButton *deleteButton;
@property (weak, nonatomic) IBOutlet UIButton *modifyButton;

@end

@implementation OrderCell

- (void)awakeFromNib {
    self.totalPrice.hidden = NO;
    self.deleteButton.hidden = YES;
    self.modifyButton.hidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ifEdit:) name:@"orderEdit" object:nil];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setIfSelected:(BOOL)ifSelected {
    _ifSelected = ifSelected;
    _checkButton.selected = _ifSelected;
}

#pragma mark - action Methods
- (IBAction)checkButton:(id)sender {
    _ifSelected = !_ifSelected;
    _checkButton.selected = _ifSelected;
    [_delegate didSelectCheckButtonAtIndexPath:_indexPath ifcheck:_ifSelected];
}

- (IBAction)deleteButton:(id)sender {
    [_delegate didSelectDeleteAtIndexPath:_indexPath];
}

- (IBAction)modifyButton:(id)sender {
    [_delegate didSelectModifyAtIndexPath:_indexPath];
}

- (void)setIfEdit:(BOOL)ifEdit {
    _ifEdit = ifEdit;
    if (_ifEdit) {
        self.totalPrice.hidden = YES;
        self.deleteButton.hidden = NO;
        self.modifyButton.hidden = NO;
    } else {
        self.totalPrice.hidden = NO;
        self.deleteButton.hidden = YES;
        self.modifyButton.hidden = YES;
    }
}

- (void)ifEdit:(NSNotification *)aNotification {
    if (aNotification) {
        if ([aNotification.object isEqualToString:@"1"]) {
            self.totalPrice.hidden = YES;
            self.deleteButton.hidden = NO;
            self.modifyButton.hidden = NO;
        } else {
            self.totalPrice.hidden = NO;
            self.deleteButton.hidden = YES;
            self.modifyButton.hidden = YES;
        }
    }
}

- (void)setModel:(ShoppingCartModel *)model {
    _model = model;
    [self.image setImageWithURL:[NSURL URLWithString:[UIUtil containString:_model.image]] placeholderImage:[UIImage imageNamed:@"tupian"]];
    self.productName.text = _model.productName;
    self.price.text = [NSString stringWithFormat:@"￥%.2f",[_model.perPrice floatValue]];
    self.count.text = [NSString stringWithFormat:@"X%@",_model.totalQuantity];
    self.totalPrice.text = [NSString stringWithFormat:@"%.2f",[_model.totalPrice floatValue]];
}

@end
